# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=gtk-doc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.34.0
pkgrel=2
pkgdesc="Documentation tool for public library API (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.gtk.org/gtk-doc/"
license=("GPL" "FDL")
depends=("${MINGW_PACKAGE_PREFIX}-docbook-xsl"
         "${MINGW_PACKAGE_PREFIX}-docbook-xml"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-pygments")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-itstool"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-cc")
url="https://www.gtk.org/gtk-doc/"
source=(https://download.gnome.org/sources/${_realname}/${pkgver%.*}/${_realname}-${pkgver}.tar.xz
        0001-exeext.patch
        pyscript2exe.py)
sha256sums=('b20b72b32a80bc18c7f975c9d4c16460c2276566a0b50f87d6852dff3aa7861c'
            '4256b893bfb75c2c0feabf9f941e9be06d0e8a756205e0a21b51896b8917c183'
            'e57174517b08cf8f9fb4f43a381d342d23d2db3cad661107f35ca21c39b5734d')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i "${srcdir}"/0001-exeext.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson \
    --prefix="${MINGW_PREFIX}" \
    --default-library shared \
    --buildtype plain \
    -Dyelp_manual=false \
    -Dtests=false \
    ../${_realname}-${pkgver}

  meson compile
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" meson install

  # Make relocatable (hacky... :/)
  for name in check fixxref mkdb mkhtml mkman mkpdf rebase scan scangobj depscan mkhtml2; do
    sed -s "s|'${MINGW_PREFIX}|sys.prefix + '|g" -i "${pkgdir}${MINGW_PREFIX}/bin/gtkdoc-${name}"
  done

  for name in check fixxref mkdb mkhtml mkman mkpdf rebase scan scangobj depscan mkhtml2; do
    ${MINGW_PREFIX}/bin/python \
      "${srcdir}/pyscript2exe.py" "${pkgdir}${MINGW_PREFIX}/bin/gtkdoc-${name}"
  done

  local _config_data="${pkgdir}${MINGW_PREFIX}/share/gtk-doc/python/gtkdoc/config_data.py"
  sed '1i\import sys' -i  "${_config_data}"
  sed -s "s|'$(cygpath -m ${MINGW_PREFIX})|sys.prefix + '|g" -i "${_config_data}"
  sed -s "s|'$(cygpath -w ${MINGW_PREFIX}/ | sed 's/\\/\\\\/g')|sys.prefix + '/|g" -i "${_config_data}"
  sed -s "s|'${MINGW_PREFIX}|sys.prefix + '|g" -i "${_config_data}"
}
